﻿namespace Multiplication
{
    internal class Program
    {
        static void Main(string[] args)
        {
            getpara();
            Test();
            Console.WriteLine("2021年实际的GDP是：17734062645371");


        }
        static double b;
        static double a;
        //求最小二乘法的参数
        static void getpara()
        {
            int[] year = {
2020,
2019,
2018,
2017,
2016,
2015,
2014,
2013,
2012,
2011,
2010,
2009,
2008,
2007,
2006,
2005,
2004,
2003,
2002,
2001,
2000,
1999,
1998,
1997,
1996,
1995,
1994,
1993,
1992,
1991,
1990,
1989,
1988,
1987,
1986,
1985,
1984,
1983,
1982,
1981,
1980,
1979,
1978,
1977,
1976,
1975,
1974,
1973,
1972,
1971,
1970,
1969,
1968,
1967,
1966,
1965,
1964,
1963,
1962,
1961,
1960
};
            double[] GDP = {
14687673892882,
14279937500608,
13894817549380,
12310409370894,
11233276536744,
11061553079871,
10475682920597,
9570406235659,
8532229986993,
7551500124197,
6087163874510,
5101703073088,
4594307032667,
3550342737009,
2752131773358,
2285965892364,
1955347004965,
1660287965663,
1470550015077,
1339395718862,
1211346869600,
1093997267277,
1029043097558,
961603952954,
863746717507,
734547898224,
564324670008,
444731282435,
426915712715,
383373318083,
360857912565,
347768051311,
312353631207,
272972974764,
300758100107,
309488028132,
259946510957,
230686747153,
205089699858,
195866382432,
191149211575,
178280594413,
149540752829,
174938098826,
153940455341,
163431551779,
144182133387,
138544284708,
113687586299,
99800958648,
92602973434,
79705906247,
70846535055,
72881631326,
76720285969,
70436266146,
59708343488,
50706799902,
47209359005,
50056868957,
59716467625, };
            for (int i = 0; i < GDP.Length; i++)
            { GDP[i] = Math.Log(GDP[i]); }//将y轴变为ln y} 

            double averx = year.Average(); //求x轴的平均数
            double avery = GDP.Average(); //求y轴的平均数
          
            double numerator = 0;//b的分子
            double denominator = 0;//b的分母
            for (int i = 0; i < 61; i++)
            {
                double numrator1 = ((double)year[i] - averx) * (GDP[i] - avery);
                numerator += numrator1;//求分子
                double denominator1 = ((double)year[i] - averx) * ((double)year[i] - averx);
                denominator += denominator1;//求分母
            }
           
            b = numerator / denominator;
            a = avery - (b * averx);
           
        }
        static void Test() //用2021年GDP检验求得的参数的准确性
        {
            int x = 2022;
            double y = (a + b * x);
            y = Math.Pow(Math.E, y);
            Console.WriteLine($"2021年预测的GDP是：{y}");
        }
    }
}